﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dao;
using System.Text;

namespace Web.Page
{
    public partial class ZL_WL_Add : BasePage
    {
        // 下拉框数组
        protected List<DropDownList> listDropDownList;
        // HtmlRow数组
        protected static List<TableRowItem> listItems = new List<TableRowItem>();
        protected List<Label> listLabel;
        protected List<HtmlTableRow> listTableRow;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "新增物料";
                initValue();
            }
            InitAllList();
        }
        private void InitAllList()
        {
            listDropDownList = new List<DropDownList>();
            listDropDownList.Add(DropDownList1);
            listDropDownList.Add(DropDownList2);
            listDropDownList.Add(DropDownList3);
            listDropDownList.Add(DropDownList4);
            listDropDownList.Add(DropDownList5);
            listDropDownList.Add(DropDownList6);
            listDropDownList.Add(DropDownList7);
            listDropDownList.Add(DropDownList8);

            listLabel = new List<Label>();
            listLabel.Add(Label1);
            listLabel.Add(Label2);
            listLabel.Add(Label3);
            listLabel.Add(Label4);
            listLabel.Add(Label5);
            listLabel.Add(Label6);
            listLabel.Add(Label7);
            listLabel.Add(Label8);

            listTableRow = new List<HtmlTableRow>();
            listTableRow.Add(tr1);
            listTableRow.Add(tr2);
            listTableRow.Add(tr3);
            listTableRow.Add(tr4);
            listTableRow.Add(tr5);
            listTableRow.Add(tr6);
            listTableRow.Add(tr7);
            listTableRow.Add(tr8);

        }

        private void initValue()
        {
            CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
            List<CS_WLLX> lsWLLX = daoWLLX.QueryAll();
            this.ddlLXBH.DataSource = lsWLLX;
            this.ddlLXBH.DataTextField = "MC";
            this.ddlLXBH.DataValueField = "BH";
            this.ddlLXBH.DataBind();
            this.ddlLXBH.Items.Insert(0, new ListItem("请选择", "0"));

            CS_JBDW_Dao daoJBDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsJBDW = daoJBDW.QueryAll();
            this.ddlDWBH.DataSource = lsJBDW;
            this.ddlDWBH.DataValueField = "BH";
            this.ddlDWBH.DataTextField = "MC";
            this.ddlDWBH.DataBind();
            this.ddlDWBH.Items.Insert(0, new ListItem("请选择", "0"));

            ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            List<ZL_GYS> lsGYS = daoGYS.QueryAll();
            this.ddlGYSID.DataSource = lsGYS;
            this.ddlGYSID.DataValueField = "ID";
            this.ddlGYSID.DataTextField = "MC";
            this.ddlGYSID.DataBind();
            this.ddlGYSID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.lbResult.Text = "";

            if (this.ddlDWBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择单位";
                return;
            }
            if (this.ddlLXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择类型";
                return;
            }
            if (this.ddlGYSID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择供应商";
                return;
            }

            ZL_WL entity = new ZL_WL()
            {
                ID = Guid.NewGuid(),
                BH = this.tbBH.Text.Trim(),
                MC = this.tbMC.Text.Trim(),
                GG = this.tbGG.Text.Trim(),
                DWBH = this.ddlDWBH.SelectedValue,
                LXBH = this.ddlLXBH.SelectedValue,
                GYSID = Guid.Parse(this.ddlGYSID.SelectedValue)
            };

            //添加属性
            foreach (TableRowItem item in listItems)
            {
                if (item.Enable)
                {
                    int c = item.Index + 1;
                    switch (c)
                    {
                        case 1:
                            entity.SXBH1 = item.Key;
                            entity.SXMC1 = item.Value;
                            entity.SXZBH1 = DropDownList1.SelectedValue;
                            entity.SXZMC1 = DropDownList1.SelectedItem.Text;
                            break;
                        case 2:
                            entity.SXBH2 = item.Key;
                            entity.SXMC2 = item.Value;
                            entity.SXZBH2 = DropDownList2.SelectedValue;
                            entity.SXZMC2 = DropDownList2.SelectedItem.Text;
                            break;
                        case 3:
                            entity.SXBH3 = item.Key;
                            entity.SXMC3 = item.Value;
                            entity.SXZBH3 = DropDownList3.SelectedValue;
                            entity.SXZMC3 = DropDownList3.SelectedItem.Text;
                            break;
                        case 4:
                            entity.SXBH4 = item.Key;
                            entity.SXMC4 = item.Value;
                            entity.SXZBH4 = DropDownList4.SelectedValue;
                            entity.SXZMC4 = DropDownList4.SelectedItem.Text;
                            break;
                        case 5:
                            entity.SXBH5 = item.Key;
                            entity.SXMC5 = item.Value;
                            entity.SXZBH5 = DropDownList5.SelectedValue;
                            entity.SXZMC5 = DropDownList5.SelectedItem.Text;
                            break;
                        case 6:
                            entity.SXBH6 = item.Key;
                            entity.SXMC6 = item.Value;
                            entity.SXZBH6 = DropDownList7.SelectedValue;
                            entity.SXZMC7 = DropDownList7.SelectedItem.Text;
                            break;
                        case 7:
                            entity.SXBH7 = item.Key;
                            entity.SXMC7 = item.Value;
                            entity.SXZBH7 = DropDownList7.SelectedValue;
                            entity.SXZMC7 = DropDownList7.SelectedItem.Text;
                            break;
                        case 8:
                            entity.SXBH8 = item.Key;
                            entity.SXMC8 = item.Value;
                            entity.SXZBH8 = DropDownList8.SelectedValue;
                            entity.SXZMC8 = DropDownList8.SelectedItem.Text;
                            break;
                    }
                }
            }

            string resultMessage = "";
            try
            {
                ZL_WL_Dao dao = new ZL_WL_Dao();
                int result = dao.Add(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.tbBH.Text = "";
            this.tbMC.Text = "";
            this.ddlLXBH.Items.FindByValue("0").Selected = true;
            this.tr1.Visible = false;
            this.tr2.Visible = false;
            this.tr3.Visible = false;
            this.tr4.Visible = false;
            this.tr5.Visible = false;
            this.tr6.Visible = false;
            this.tr7.Visible = false;
            this.tr8.Visible = false;
        }

        protected void btnBH_Click(object sender, EventArgs e)
        {
            //            物料编码：（编号是变长的）材料类型（2位）+供应商编号（4位）+属性1编号（2位）+属性2编号（2位）+...
            //例子：板材(01)+供应商1(0001)+刨花板(01)+双面(02)+银灰(104)+厚度12(05)+尺寸4*8(01)
            if (ddlLXBH.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择类型";
                return;
            }
            if (ddlGYSID.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择供应商";
                return;
            }
            StringBuilder sb = new StringBuilder();
            string cllx = ddlLXBH.SelectedValue.Substring(0, 2);
            sb.Append(cllx);
            string gys = ddlGYSID.SelectedValue.Substring(0, 4);
            sb.Append(gys);

            foreach (TableRowItem item in listItems)
            {
                if (item.Enable)
                {
                    int i = item.Index;
                    if (listDropDownList[i].SelectedValue == "0")
                        continue;
                    sb.Append(item.Key.Trim());
                }
            }

            this.tbBH.Text = sb.ToString();

        }

        protected void ddlLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 根据类型获取属性
            string type = ddlLXBH.SelectedValue;

            CS_WLLXSX_Dao daoWLLXSX = new CS_WLLXSX_Dao();
            List<CS_WLLXSX> listWLLXSX = daoWLLXSX.Query(type);
            listItems.Clear();
            if (listWLLXSX.Count > 0)
            {
                // 将8个属性下拉框依次初始化
                for (int i = 0; i < listWLLXSX.Count; i++)
                {
                    string key = listWLLXSX[i].BH;
                    string value = listWLLXSX[i].MC;
                    DropDownList ddl = listDropDownList[i];
                    Label lbl = listLabel[i];
                    HtmlTableRow tr = listTableRow[i];
                    TableRowItem tri = new TableRowItem(i, key, value, ddl, lbl, tr, this.ddlLXBH.SelectedValue);
                    listItems.Add(tri);
                }

                // 显示row
                foreach (TableRowItem item in listItems)
                {
                    item.Tr.Visible = item.Enable;
                }

            }
            else
            {
                tr1.Visible = false;
                tr2.Visible = false;
                tr3.Visible = false;
                tr4.Visible = false;
                tr5.Visible = false;
                tr6.Visible = false;
                tr7.Visible = false;
                tr8.Visible = false;
                //for (int i = 0; i < listItems.Count; i++)
                //{
                //    listItems[i].Enable = false;
                //    listItems[i].Tr.Visible = false;
                //}
            }
        }

        protected class TableRowItem
        {
            public int Index { get; set; }
            public HtmlTableRow Tr { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public DropDownList Ddl { get; set; }
            public List<CS_WLSXZ> Data { get; set; }
            public string Wllxbh { get; set; }
            public Label Lbl { get; set; }

            public bool Enable { get; set; }

            public TableRowItem(int index, string key, string value, DropDownList ddl, Label label, HtmlTableRow tr, string wllxbh)
            {
                this.Index = index;
                this.Key = key;
                this.Value = value;
                this.Ddl = ddl;
                this.Wllxbh = wllxbh;
                this.Lbl = label;
                this.Tr = tr;
                Init();
            }

            private void Init()
            {
                this.Lbl.Text = Value;

                // 获取属性值
                CS_WLSXZ_Dao daoWLSXZ = new CS_WLSXZ_Dao();
                List<CS_WLSXZ> list = daoWLSXZ.Query(this.Wllxbh, this.Key);
                this.Ddl.Items.Clear();
                if (list.Count > 0)
                {
                    this.Ddl.DataSource = list;
                    this.Ddl.DataTextField = "MC";
                    this.Ddl.DataValueField = "BH";
                    this.Ddl.DataBind();
                    this.Enable = true;
                }
                else
                {
                    this.Enable = false;
                }
                this.Ddl.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

    }
}